﻿using DeviceManager.ContentPage.SysMng;
using DeviceManager.DB;
using DeviceManager.DB.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace DeviceManager.ContentPage.UserMng
{
    /// <summary>
    /// EditDepInfo.xaml 的交互逻辑
    /// </summary>
    public partial class EditDepInfo : Window
    {

        internal DepmentInfo depInfo = null;

        public EditDepInfo()
        {
            InitializeComponent();
        }

        private void blt_Save_Click(object sender, RoutedEventArgs e)
        {
            if (!(tb_parentdep.Tag is DepmentInfo))
            {
                MessageBox.Show("请选择上级组织机构");
                return;
            }
            if(string.IsNullOrEmpty(tb_depName.Text.Trim(' '))){
                MessageBox.Show("新建组织机构信息必须填入一个名称");
                return;
            }
            DepmentInfo editDepInfo = new DepmentInfo();
            if (depInfo != null)
            {
                editDepInfo.Id = depInfo.Id;
            }
            else
            {
                editDepInfo.Id = 0;
            }
            editDepInfo.Name = tb_depName.Text;
            editDepInfo.pid = (tb_parentdep.Tag as DepmentInfo).Id;
            editDepInfo.Des = tb_depDes.Text;
            if (ManagerDBCtrl.GetMng().SaveDepmentInfo(editDepInfo))
            {
                depInfo = editDepInfo;
                this.DialogResult = true;
            }
            else
            {
                MessageBox.Show("保存数据失败");
            }
        }

        internal void SetEdit(DepmentInfo depmentInfo)
        {
            if (depmentInfo == null)
            {
                return;
            }
            depInfo = depmentInfo;
            this.tb_depDes.Text = depInfo.Des;
            this.tb_depName.Text = depInfo.Name;
            DepmentInfo parentDep = ManagerDBCtrl.GetMng().GetDepmentInfoByID(depInfo.pid);
            tb_parentdep.Tag = parentDep;
            if (parentDep != null)
            {
                tb_parentdep.Text = parentDep.Name;
                tb_parentdep.ToolTip = parentDep.Des;
            }
            this.Tag = depInfo;
        }

        private void blt_Cannel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void blt_selectParetnDep_Click(object sender, RoutedEventArgs e)
        {
            WindowDepSelected windowSelect = new WindowDepSelected();
            windowSelect.Owner = this;
            windowSelect.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            if (windowSelect.ShowDialog() == true)
            {
                tb_parentdep.Tag = windowSelect.NowSelectedInfo;
                tb_parentdep.Text = windowSelect.NowSelectedInfo.Name;
                tb_parentdep.ToolTip = windowSelect.NowSelectedInfo.Des;
            }
        }

        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                this.DragMove();
            }
            catch { } 
        }
    }
}
